<?php

/**
 * Description of DALCountry
 *
 * @author aethv
 */
class DALConfig extends DALBase {

    const SEPERATE = 'xx00xx';
    public $dalLeague;
    public $dalCountry;
    public $dalUser;

    public function __construct() {
        parent::__construct();
        $this->_tbl = 'config';
        $this->_key = 'id';
        $this->dalCountry = new DALCountry();
        $this->dalLeague = new DALLeague();
        $this->dalUser = new DALUserAccount();
    }
    
    public function save($data) {
        $count = 0;
        //get all config
        $lstConfig = $this->loadList('id', 'ASC', '-1');
        
        //get config
        $list = explode('v0v', $data);
        $betTimes = $list[0];
        $setting = $list[1];
        
        $query = "UPDATE $this->_tbl SET `value`='$betTimes' WHERE id=1;";
        $this->executeQuery($query);
        $count++;
        
        $query = "UPDATE $this->_tbl SET `value`='$setting' WHERE id=2;";
        $this->executeQuery($query);
        $count++;
            
        
        return $count;
    }
    
    function getBetRules(){
        $query = "SELECT * FROM $this->_tbl WHERE `name` IN ('BET_TIMES', 'BET_SETTING');";
        $result = $this->executeQuery($query);
        $ret = array();
        if($result){
            while ($row = mysql_fetch_object($result)) {
                //  put row object into the array
                $ret[] = $row;
            }
        }
        return $ret;
    }
    
    function getConfigSetting() {
        $ret = '';
        $countryId = '';
        $leagueId = '';
        $config = '';

        //get countries
        $countries = $this->dalCountry->getByProperty('status', '1');
        foreach ($countries as $country) {
            $countryId .= ",$country->id";
        }
        $cList = (strlen($countryId) > 0) ? substr($countryId, 1) : '';

        //get league
        $leagues = $this->dalLeague->getByProperty('status', '1');
        foreach ($leagues as $league) {
            $leagueId .= ",$league->id";
        }
        $lList = (strlen($leagueId) > 0) ? substr($leagueId, 1) : '';

        //get user info
        $userAccount = $this->dalUser->getByProperty('username', 'admin');

        //get config setting
        $betTimes = '';
        $betSetting = '';
        $lstConfig = $this->loadList('id', 'ASC', -1);
        foreach ($lstConfig as $config) {
            if($config->name === 'BET_TIMES'){
                $betTimes = $config->value;
            }else if($config->name === 'BET_SETTING'){
                $betSetting = $config->value;
            }
        }
        $config = $betTimes . 'xx00xx' . $betSetting;
        
        $ret = $cList . 'xx00xx' . $lList . 'xx00xx' . $userAccount[0]->active . 'xx00xx' . $config;

        return $ret;
    }

}
